package gncom.qr;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class ParseJSON {
	
	private String data;
	
	public void executeHttpPost_board() {
		
		HttpClient client = new DefaultHttpClient();
		String urlAddress_remote = "http://mobile.gncom.co.kr:8080/qr25/mobile/board_list1.jsp?uuid=359719661799248";
		HttpPost request = new HttpPost(urlAddress_remote);

		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		
		
		request.setEntity(makeEntity(nameValue));
		
		BufferedReader br = null;
		
		try {
			HttpResponse response = client.execute(request);
			
			//Toast.makeText(this, response.toString(), 0).show();
			
			///////////euc-kr  한글 처리
			br = new BufferedReader(
					new InputStreamReader(response.getEntity().getContent(),"euc-kr"));
			
			StringBuffer sb = new StringBuffer();
			String line = "";
			String sysProf = System.getProperty("line.separator");
			while((line = br.readLine()) != null){
				if(line.indexOf("Result:") >=0 ){
					sb.append(line.substring(7)+ sysProf);
				}
				Log.i("http", line);
				data = line;
			}
			
			//Log.i("http", "Respone : " + sb.toString());
			
			

			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	private HttpEntity makeEntity(ArrayList<NameValuePair> nameValue) {
		// TODO Auto-generated method stub
		HttpEntity result = null;

		try {
			result = new UrlEncodedFormEntity(nameValue, "euc-kr");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	

	public Vector<ParseJSON_VO_board> getParse(){
		Vector<ParseJSON_VO_board> list = new Vector<ParseJSON_VO_board>();
		
		
		data = data.replace("\\", "");
		Log.i("qr", data);
		try {
			JSONArray mJsonArray = new JSONArray(data);
			for (int i = 0; i < mJsonArray.length(); i++) {
				ParseJSON_VO_board vo = new ParseJSON_VO_board();
				JSONObject mJsonObject = (JSONObject) mJsonArray.get(i);
				vo.setHit(mJsonObject.getString("hit"));
				vo.setSubject(mJsonObject.getString("subject"));
				vo.setContent(mJsonObject.getString("content"));
				vo.setNo(mJsonObject.getString("no"));
				vo.setWdate(mJsonObject.getString("wdate"));
				vo.setWriter(mJsonObject.getString("writer"));
				
				list.add(vo);
				
				//Log.i("http", "subject : " + mJsonObject.getString("subject"));
			}
		
		} catch (JSONException e) {
		}
		
		return list;
	}
	
}
